package net.lab0.nebula.gui;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import net.lab0.nebula.core.QuadTreeManager;
import net.lab0.nebula.data.Statistics;

@SuppressWarnings("serial")
public class StatisticsPanel extends JPanel
{
    private JPanel     pnlFixed;
    private JPanel     pnlPerDepthChart;
    
    private MainWindow mainWindow;
    
    //useful for window builder
    @SuppressWarnings("unused")
    private StatisticsPanel()
    {
        this(null);
    }
    
    public StatisticsPanel(MainWindow mainWindow)
    {
        this.mainWindow = mainWindow;
        
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.getVerticalScrollBar().setUnitIncrement(16);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        this.add(scrollPane);
        
        JPanel panel_3 = new JPanel();
        scrollPane.setViewportView(panel_3);
        panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.PAGE_AXIS));
        
        pnlFixed = new JPanel();
        panel_3.add(pnlFixed);
        
        pnlPerDepthChart = new JPanel();
        panel_3.add(pnlPerDepthChart);
        pnlPerDepthChart.setLayout(new BoxLayout(pnlPerDepthChart, BoxLayout.PAGE_AXIS));
    }
    
    public void updateStatisticsPanel()
    {
        QuadTreeManager quadTreeManager = mainWindow.getQuadTreeManager();
        
        if (quadTreeManager == null)
        {
            mainWindow.showNoQuadTreeError();
            return;
        }
        
        pnlFixed.removeAll();
        pnlPerDepthChart.removeAll();
        
        Statistics statistics = quadTreeManager.computeStatistics();
        pnlFixed.add(ChartsFactory.createGeneralStatisticsPanel(statistics));
        pnlPerDepthChart.add(ChartsFactory.createPerLayerGraph(statistics));
        
        pnlFixed.revalidate();
        pnlPerDepthChart.revalidate();
    }
}
